<?php

class pics_top extends Model
{
	public function __construct()
	{
		//初始化
		$this->init_top();
	}

	//获取图片
	public function PictureGetRequest($title="",$pageno=1,$pagesize=10,$cid=0)
	{
		$req = new PictureGetRequest;

		if($cid) $req->setPictureCategoryId($cid);
		if($title) $req->setTitle($title);
		$req->setPageNo($pageno);
		$req->setPageSize($pagesize);
		$resp = $this->c->execute($req, $this->top_session);
		return $resp;
	}

	//获取图片分类  --------  暂时不需要传入参数
	//type = fixed代表店铺装修分类类别，auction代表宝贝分类类别，user-define代表用户自定义分类类别
	public function PictureCategoryGetRequest($picture_category_id=0,$picture_category_name='',$type='',$parent_id=-1,$modified_time='')
	{
		$req = new PictureCategoryGetRequest;

		$resp = $this->c->execute($req, $this->top_session);
		return $resp;
	}



	public function find_picture($pid){
		$req = new PicturePicturesGetRequest;
		$req->setTitle($pid.'.png');
		$resp = $this->c->execute($req, $this->top_session);

		return $resp;
	}

	//上传图片
	//@param $id 目录ID
	//@param $cover_url 上传图片的本地url
	//@return $resp 返回上传结果
	public function upload_picture($tu_name,$pid,$cover_path){
		$cat_id = $this->get_picture_category($tu_name);

		$pic = '';

		$picture = $this->find_picture($pid);

		if(isset($picture->pictures->picture[0])){
			$req = new PictureReplaceRequest;
			$req->setPictureId($picture->pictures->picture[0]->picture_id);
			$req->setImageData('@'.$cover_path);
			$pic = $this->c->execute($req, $this->top_session);
			if($pic->done) {
				$pic->picture = (object)array();
				$pic->picture->picture_path = $picture->pictures->picture[0]->picture_path;
			}
		}else{
			$req = new PictureUploadRequest;
			$req->setPictureCategoryId($cat_id);
			$req->setImg('@'.$cover_path);
			$req->setImageInputTitle($pid.'.png');
			$pic = $this->c->execute($req, $this->top_session);
		}

		return $pic;
	}

	public function del_picture($pid){
		$resp = '';

		$picture = $this->find_picture($pid);
		if($picture->pictures->picture[0]->picture_id){
			$req = new PictureDeleteRequest;
			$req->setPictureIds($picture->pictures->picture[0]->picture_id);
			$resp = $this->c->execute($req, $this->top_session);
		}

		return $resp;
	}
}